﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AIC_Notification
{
    class GuidanceStudentStatusForAC: FormatedReport
    {
        public IList<Guidance_Application> Students = null;
        public string TeacherInContext;
        public int FontSize = 14;
        public string[] MonthString = new string[12] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

        private int CurrentYear = Global.Now().Year;
        private int CurrentMonth = Global.Now().Month;
        private int CurrentDay = Global.Now().Day;
        private int PreviousMonth;
        private int PreviousYear;

        public GuidanceStudentStatusForAC()
        {
            ReportTitle = "Guidance Student Status (Consultant)";

            PreviousMonth = CurrentMonth - 1 == 0 ? 12 : CurrentMonth - 1;
            PreviousYear = PreviousMonth == 12 ? CurrentYear - 1 : CurrentYear;
        }

        public override string GetSubject()
        {
            return String.Format("{0} - {1} - {2:d}", ReportTitle, SubjectID, Global.Now());
        }

        public override string GenerateLedger()
        {
            string htmlLedger = "";

            htmlLedger += "<tr><td>";
            htmlLedger += "<div style=\"text-align:center; font-size: 11px;\">";
            htmlLedger += "<b><u>Monthly Plan</u></b>: Submit before <b style=\"color: red\">5th</b> of each month";
            htmlLedger += "</div>";
            htmlLedger += "<div style=\"text-align:center; font-size: 11px;\">";
            htmlLedger += "<b><u>Meeting Recap</u></b>: <b style=\"color: red\">2 recaps</b> per month expected";
            htmlLedger += "</div>";
            htmlLedger += "</td></tr>";

            return htmlLedger;
        }

        public override string InnerTableHeader()
        {
            string tableHeader = "";
            bool DisplayReportCard = (PreviousMonth == 12 || PreviousMonth == 4 || PreviousMonth == 8) ? true : false;
            string TermName = "";

            if (PreviousMonth == 12)
                TermName = "Fall";
            else if (PreviousMonth == 4)
                TermName = "Spring";
            else if (PreviousMonth == 8)
                TermName = "Summer";

            // Add Student Name column
            tableHeader += "<tr>";
            tableHeader += String.Format("<th style=\"background-color: #483D8B; color: white; font-size: {0}px;\">Students</th>", FontSize);
            tableHeader += String.Format("<th style=\"background-color: #483D8B; color: white; font-size: {0}px;\">Score</th>", FontSize);
            tableHeader += String.Format("<th style=\"background-color: #483D8B; color: white; font-size: {0}px;\">SAT2 and AP</th>", FontSize);
            tableHeader += String.Format("<th style=\"background-color: #483D8B; color: white; font-size: {0}px;\">Monthly Plan Progress</th>", FontSize);
            tableHeader += String.Format("<th style=\"background-color: #483D8B; color: white; font-size: {0}px;\">Meeting Recap</th>", FontSize);
            if (DisplayReportCard)
                tableHeader += String.Format("<th style=\"background-color: #483D8B; color: white; font-size: {0}px;\">Term Report Card</th>", FontSize);
            tableHeader += String.Format("<th style=\"background-color: #483D8B; color: white; font-size: {0}px;\">Monthly Plan Progress</th>", FontSize);
            tableHeader += String.Format("<th style=\"background-color: #483D8B; color: white; font-size: {0}px;\">Meeting Recap</th>", FontSize);
            tableHeader += "</tr>";

            tableHeader += "<tr>";
            tableHeader += String.Format("<td style=\"background-color: #B0E2FF; font-size: {0}px;\"><u>Month</u></td>", FontSize);
            tableHeader += String.Format("<td style=\"background-color: #B0E2FF; font-size: {0}px;\"></td>", FontSize);
            tableHeader += String.Format("<td style=\"background-color: #B0E2FF; font-size: {0}px;\"></td>", FontSize);
            tableHeader += String.Format("<td style=\"background-color: #B0E2FF; font-size: {0}px;\">{1}</td>", FontSize, MonthString[PreviousMonth - 1]);
            tableHeader += String.Format("<td style=\"background-color: #B0E2FF; font-size: {0}px;\">{1}</td>", FontSize, MonthString[PreviousMonth - 1]);
            if (DisplayReportCard)
                tableHeader += String.Format("<td style=\"background-color: #B0E2FF; font-size: {0}px;\">{1}</td>", FontSize, TermName);
            tableHeader += String.Format("<td style=\"background-color: #B0E2FF; font-size: {0}px;\">{1}</td>", FontSize, MonthString[CurrentMonth - 1]);
            tableHeader += String.Format("<td style=\"background-color: #B0E2FF; font-size: {0}px;\">{1}</td>", FontSize, MonthString[CurrentMonth - 1]);
            tableHeader += "</tr>";

            return tableHeader;
        }

        private string InsertMonthPlanColumn(Guidance_Application student, int Year, int Month)
        {
            string MPColumnHTML = "";

            if (student.MonthlyPlanList != null)
            {
                string CellHTML = "";
                string MonthPlanHTML = "";
                string CheckListHTML = "";
                string DateHTML = "";
                bool HasMonthlyPlan = false;
                bool HasChecklist = false;
                string CellColor = "";

                foreach (var MonthlyPlan in student.MonthlyPlanList.MonthlyPlanList)
                {
                    if (MonthlyPlan.Month == Month && MonthlyPlan.Year == Year) // Found Card
                    {
                        MonthPlanHTML = String.Format("<div><a href=\"{0}\">Plan</a></div>", MonthlyPlan.Url);
                        CheckListHTML = "";
                        if (MonthlyPlan.PlanCheckList != null) // Has Checklist
                        {
                            CheckListHTML = String.Format("Completion: {0}/{1}", MonthlyPlan.PlanCheckList.ItemsComplete(), MonthlyPlan.PlanCheckList.TotalItems());
                            HasChecklist = true;
                        }
                        else
                        {
                            CheckListHTML = "<div>Missing Checklist</div>";
                        }
                        DateHTML = String.Format("<div style=\"font-size: {0}px;\">({1:yyyy-MM-dd})</div>", FontSize - 2, MonthlyPlan.SubmitDate);
                        HasMonthlyPlan = true;
                    }
                }

                if (HasMonthlyPlan) // Has Card
                {
                    if (HasChecklist) // Has Checklist
                    {
                        CellColor = "background-color: #CCFFCC;"; // Green
                        CellHTML = MonthPlanHTML + CheckListHTML + DateHTML;
                    }
                    else // No Checklist
                    {
                        CellColor = "background-color: #FFFFCC;"; // Yellow
                        CellHTML = MonthPlanHTML + CheckListHTML + DateHTML;
                    }
                }
                else // No Card
                {
                    if ((Month == CurrentMonth) && (CurrentDay <= 5)) // OK if before the 5th
                    {
                        CellColor = ""; // White
                        CellHTML = "Pending";
                    }
                    else // After the 5th, Overdue.
                    {
                        CellColor = "background-color: #F8D3D3;"; // Red
                        CellHTML = "Missing";
                    }
                }
                MPColumnHTML += String.Format("<td style=\"font-size: {0}px; {1}\">{2}</td>", FontSize, CellColor, CellHTML);
            }
            else
                MPColumnHTML = "<td></td>";

            return MPColumnHTML;
        }

        private string InsertMeetingRecapColumn(Guidance_Application student, int Year, int Month)
        {
            string MRColumnHTML = "";

            if (student.MeetingRecapList != null)
            {
                int MeetingRecapCount = 0;
                string MeetingRecapHTML = "";

                foreach (var MeetingRecap in student.MeetingRecapList.MeetingRecapList)
                {
                    if (MeetingRecap.Month == Month && MeetingRecap.Year == Year)
                    {
                        string MeetingHTML = String.Format("<a href=\"{0}\">M{1}:{2}-{3}-{4}</a>", MeetingRecap.Url, MeetingRecapCount + 1, MeetingRecap.Year, MeetingRecap.Month, MeetingRecap.Day);
                        MeetingRecapHTML += String.Format("<div>{0}</div>", MeetingHTML);
                        MeetingRecapCount += 1;
                    }
                }

                if (MeetingRecapCount > 0)
                {
                    if (MeetingRecapCount == 1)
                        MRColumnHTML += String.Format("<td style=\"font-size: {0}px; background-color: #FFFFCC;\">{1}</td>", FontSize, MeetingRecapHTML);
                    else
                        MRColumnHTML += String.Format("<td style=\"font-size: {0}px; background-color: #CCFFCC;\">{1}</td>", FontSize, MeetingRecapHTML);
                }
                else
                    MRColumnHTML += String.Format("<td style=\"font-size: {0}px; background-color: #F8D3D3;\">No Update</td>", FontSize);
            }
            else
                MRColumnHTML = "<td></td>";

            return MRColumnHTML;
        }

        private string InsertReportCardColumn(Guidance_Application student, int Year, int Month)
        {
            string RCColumnHTML = "";
            bool DisplayReportCard = (Month == 12 || Month == 4 || Month == 8) && (Global.Now().Month == ((Month + 1) % 12)) ? true : false  ;
            Guidance_ReportCard.GuidanceTerm DisplayTerm = Guidance_ReportCard.GuidanceTerm.Fall; ;

            if (DisplayReportCard)
            {
                // Set Guidance Term
                if (Month == 12)
                    DisplayTerm = Guidance_ReportCard.GuidanceTerm.Fall;
                else if (Month == 4)
                    DisplayTerm = Guidance_ReportCard.GuidanceTerm.Spring;
                else if (Month == 8)
                    DisplayTerm = Guidance_ReportCard.GuidanceTerm.Summer;

                if (student.EvaluationReportCardList != null)
                {
                    string ReportCardHTML = "";
                    bool HasAttachment = false;

                    foreach (var ReportCard in student.EvaluationReportCardList.ReportCards)
                    {
                        if (ReportCard.Term == DisplayTerm && ReportCard.Year == Year)
                        {
                            ReportCardHTML = String.Format("<div><a href=\"{0}\">{1}-{2}</a></div>", ReportCard.Url, ReportCard.Year, ReportCard.Term);
                            HasAttachment = ReportCard.HasAttachment;
                            break;
                        }
                    }

                    if (ReportCardHTML != "") // Has Report Card!
                    {
                        if (HasAttachment)
                            RCColumnHTML += String.Format("<td style=\"font-size: {0}px; background-color: #CCFFCC;\">{1}</td>", FontSize, ReportCardHTML); // Green
                        else
                            RCColumnHTML += String.Format("<td style=\"font-size: {0}px; background-color: #FFFFCC;\"><div>{1}</div><div>Missing Attachment</div></td>", FontSize, ReportCardHTML); // Yellow
                    }
                    else
                    {
                        if (Global.Now().Day >= 15) // Pass mid-month
                        {
                            RCColumnHTML += String.Format("<td style=\"font-size: {0}px; background-color: #F8D3D3;\">Missing Report Card</td>", FontSize); // Red
                        }
                        else
                        {
                            RCColumnHTML += String.Format("<td style=\"font-size: {0}px;\">Missing Report Card</td>", FontSize); // White
                        }
                    }
                }
                else
                {
                    if (Global.Now().Day >= 15) // Pass mid-month
                    {
                        RCColumnHTML += String.Format("<td style=\"font-size: {0}px; background-color: #F8D3D3;\">Missing Report Card</td>", FontSize); // Red
                    }
                    else
                    {
                        RCColumnHTML += String.Format("<td style=\"font-size: {0}px;\">Missing Report Card</td>", FontSize); // White
                    }
                }
            }

            return RCColumnHTML;
        }

        public override string GenerateInnerTable()
        {
            string tableBody = "";

            //if (ShowStatistics)
            //    tableBody += InnerTableStatistics();

            if (Students.Count > 0)
            {
                IList<Guidance_Application> StudentsByName = null;
                StudentsByName = Students.OrderBy(Guidance_Application => Guidance_Application.Name).ToList(); // Order by student Name

                foreach (var student in StudentsByName)
                {
                    // Generate row content
                    tableBody += "<tr>";

                    tableBody += InsertStudentName(student, FontSize);
                    tableBody += InsertStudentScore(student, FontSize-2, true);
                    tableBody += InsertSAT2andAP(student, FontSize-2);

                    // Previous Month
                    tableBody += InsertMonthPlanColumn(student, PreviousYear, PreviousMonth);
                    tableBody += InsertMeetingRecapColumn(student, PreviousYear, PreviousMonth);
                    tableBody += InsertReportCardColumn(student, PreviousYear, PreviousMonth);

                    // Current Month
                    tableBody += InsertMonthPlanColumn(student, CurrentYear, CurrentMonth);
                    tableBody += InsertMeetingRecapColumn(student, CurrentYear, CurrentMonth);

                    tableBody += "</tr>";
                }
            }
            return tableBody;
        }

        public override void SendReportAsEmail()
        {
            foreach (var _teacher in TeacherDB.Teachers)
            {
                string HTMLBody;

                // Only for AC with one student or more
                // if ((_teacher.Type.Equals("Academic Consultant") || _teacher.Type.Equals("Both")) && _teacher.Students.Count > 0)
                if (_teacher.GuidanceStudents.Count > 0)
                {
                    Students = _teacher.GuidanceStudents;
                    TeacherInContext = _teacher.Name;
                    HeaderInformation = _teacher.Name;

                    FileName = String.Format("{0}-{1}-{2:yyyyMMdd-HHmmss}.htm", ReportTitle, _teacher.Name, Global.Now());
                    Global.SysUtil.Log("Generating Report: " + FileName);

                    HTMLBody = GetHTMLBody();

                    // Save Report to File
                    SaveReportToFile(FileName, HTMLBody);

                    // Send Report E-mail
                    toRecipients.Clear();
                    ccRecipients.Clear();

                    // Send to teacher
                    toRecipients.Add(_teacher.Email);

                    // Copy e-mail to extra recipients if not empty
                    if (!IsEmptyBody)
                    {
                    }

                    SubjectID = _teacher.Name;

                    Global.SysUtil.SendHTML(toRecipients, ccRecipients, GetSubject(), HTMLBody);
                }
            }
        }
    }
}